package database;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DBUtil {

    public static void main(String[] args) throws Exception {
//        StopWatch stopWatch = new StopWatch();
//        for (int i = 0; i < 10; i++) {
//            stopWatch.start("task : "+i);
//            doTest();
//            stopWatch.stop();
//        }
//        System.out.println(stopWatch.prettyPrint());

//       doTest2();
//        int aa = 112;
//        Class bb = int.class;
//        Object cc = getType(bb);
//        System.out.println(cc);
//        if (Number.class.isAssignableFrom((Class<?>) cc) || int.class == cc) {
//            System.out.println("aa is int");
//        }
//        else {
//            System.out.println("aa is not int .....");
//        }

//        findAllFields(database.Test.class);
//        doTest2();
//        doTest3();
//        LocalDateTime ldt = LocalDateTime.now();
//        ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault());
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println(sdf.format(Date.from(zdt.toInstant())));

//        testR2db();

//        String sql = "select * from test where id=#{id} and name=#{name} ";
//        database.Test test = new database.Test();
//        test.setId(11);
//        test.setName("aaaaa");

//        sql = ObjectParser.setParams(sql,test);
//        System.out.println(sql);

//        String reg = "#{id}";
//        System.out.println(sql.indexOf(reg));

//        doTest();
//        String aaa = "select * from test where age<#{age}";
//        String sql = aaa.replace("#{age}","$1");
//        System.out.println(sql);
//        testR2db();
    }

    public static List<Field>  findAllFields(Class<?> tempClass){
        List<Field> fieldList = new ArrayList<>() ;
        while (tempClass != null) {//当父类为null的时候说明到达了最上层的父类(Object类).
            fieldList.addAll(Arrays.asList(tempClass .getDeclaredFields()));
            tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
        }
        for (Field f : fieldList) {
            System.out.println("field: "+f.getName());
        }
        return fieldList;
    }

    public static Object getType(Object o){
        return o;
    }


    /**
     * postgresql 连接
     * @throws Exception
     */
//    public static void doTest3() throws Exception{
//        String sql = "select * from test ";
//        System.out.println("sql: "+sql);
//        Class.forName("org.postgresql.Driver");
//        Connection conn = DriverManager.getConnection("jdbc:postgresql://47.112.216.79:5432/testdb?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2b8", "curious", "Curious@123456");
//        Statement statement = conn.createStatement();
//        ResultSet rs = statement.executeQuery(sql);
////        List<database.Test> list = ObjectParser.parse(rs,database.Test.class);
////        list.forEach(System.out::println);
//        List<database.Test> test = ObjectParser.parseForResultSet(rs,database.Test.class);
//        test.forEach(System.out::println);
//        rs.close();
//        conn.close();
//    }
//
//
//
//    public static void doTest2() throws Exception{
////        database.Test params = new database.Test();
////        params.setName("王伟");
////        params.setAge(20);
////        Date aabb = cn.hutool.core.date.DateUtil.parse("2020-01-28");
////        params.setCreateDate(aabb);
//        String sql = "select * from test where id=1 ";
////        Map<String,Object> paramMap = new HashMap<>();
////        paramMap.put("createDate",aabb);
////        sql = ObjectParser.setParams(sql,params);
//
//        System.out.println("sql: "+sql);
//
//        Class.forName("com.mysql.cj.jdbc.Driver");
////        Connection conn = DriverManager.getConnection("jdbc:mysql://47.112.216.79:3306/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2b8", "root", "root");
//        Connection conn = DriverManager.getConnection("jdbc:mysql://47.112.216.79:3306/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2b8", "curious", "Curious@123456");
//        Statement statement = conn.createStatement();
//        ResultSet rs = statement.executeQuery(sql);
////        List<database.Test> list = ObjectParser.parse(rs,database.Test.class);
////        list.forEach(System.out::println);
//        database.Test test = ObjectParser.parseObjectFoResultSet(rs,database.Test.class);
//        System.out.println(test);
//        rs.close();
//        conn.close();
//    }
//
    public static void doTest() throws Exception {
        System.out.println("init---");
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://47.112.216.79:3306/test?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2b8", "curious", "Curious@123456");
        Statement statement = conn.createStatement();
        PreparedStatement preparedStatement = conn.prepareStatement("select * from test");
        String sql = "select * from test";
        ResultSet rs = statement.executeQuery("select * from test");
//        while (rs.next()) {
//            String name = rs.getString("name");
//            System.out.println(rs.getString("age") + "\t" + name);
//        }
//        List<database.Test> list = ObjectParser.parseForResultSet(sql,rs,database.Test.class);
//        list.forEach(System.out::println);
        rs.close();
        conn.close();
    }







//    public static void testR2db() {
////        Map<String, String> options = new HashMap<>();
////        options.put("lock_timeout", "10s");
//        PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(PostgresqlConnectionConfiguration.builder()
//                .host("47.112.216.79")
//                .port(5432)  // optional, defaults to 5432
//                .username("curious")
//                .password("Curious@123456")
//                .database("testdb")  // optional
////                .options(options) // optional
//                .build());
//
////        Mono<PostgresqlConnection> mono = connectionFactory.create();
//        DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
////        Flux<Map<String, Object>> rows = databaseClient.execute()
////                .sql("SELECT id, name, manual FROM legoset")
////                .fetch()
////                .all();
//
//        Flux<Map<String, Object>> all = databaseClient.execute("SELECT * FROM test where age<$1")
//                .bind("$1",20)
//                .fetch().all();
//
//        Map<String, Object> map = all.blockFirst();
//        map.forEach((key,value)->{
//            System.out.println(key+",  "+value);
//        });
//        String  meta = connectionFactory.create().block().getMetadata().getDatabaseProductName();
//
//        System.out.println("We are using " + meta);
//
//    }

}
